package com.example.home.me;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.MainActivity;
import com.example.home.R;
import com.example.home.bean.Content;
import com.example.home.bean.User;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

public class LoginActivity extends AppCompatActivity {

    private Button but_login;
    private EditText editUsername;
    private EditText editPassword;
    private User user;
    private Handler handler;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        initView();
    }

    private void getLoginData(String username, String password) {
        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        // 生成JSON数据
        String jsonU = gson.toJson(user);

        OKHttpTool.doNetRequest("prod-api/api/login",
                OKHttpTool.POST,
                Content.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        Content content = (Content) obj;
                        if (content.getCode() == 200) {
                            OKHttpTool.TOKEN = content.getToken();
                            save(content.getToken());
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    bindLogin();
                                }
                            });
                        }else if (content.getCode() == 500){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 在此处理失败情况
                        Log.e("LoginActivity-","失败");
                    }
                }, jsonU
        );
    }


    private void bindLogin() {
        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra("isToMeFragment", "y");
        startActivity(intent);
        LoginActivity.this.finish();
    }

    private void initView() {
        but_login = findViewById(R.id.but_login);
        editUsername = findViewById(R.id.editTextUsername);
        editPassword = findViewById(R.id.editTextPassword);
        but_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户名和密码
                String username = editUsername.getText().toString();
                String password = editPassword.getText().toString();
                // 检查数据是否合法和不为空
                if (!username.isEmpty() && !password.isEmpty()) {
                    getLoginData(username, password);
                } else {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        handler = new Handler();
        gson = new Gson();
    }

    private void save(String authToken) {
        // 获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE);
        // 获取SharedPreferences编辑器
        SharedPreferences.Editor editor = sharedPreferences.edit();
        // 保存认证令牌作为键值对
        editor.putString("auth_token", authToken);
        // 应用更改
        editor.apply();
        OKHttpTool.TOKEN = authToken;
    }


}
